package com.yunze.task.yunze.bill;

import com.alibaba.fastjson.JSON;
import com.yunze.common.config.RabbitMQConfig;
import com.yunze.common.core.domain.model.LoginUser;
import com.yunze.common.mapper.mysql.bill.YzBillMapper;
import com.yunze.common.mapper.mysql.bill.YzBillsMapper;
import com.yunze.common.utils.ServletUtils;
import com.yunze.common.utils.spring.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.security.SecurityUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;


import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.*;

@Slf4j
@Component
public class BillTaskMQ {

    @Resource
    private YzBillMapper yzBillMapper;

    @Resource
    private YzBillsMapper yzBillsMapper;

    @Resource
    private RabbitMQConfig rabbitMQConfig;

    String polling_queueName = "admin_Bill_queue";
    String polling_routingKey = "admin.Bill.queue";
    String polling_exchangeName = "admin_exchange";//路由

    /**
     * 账单 每月 一次 进行新增
     *
     */
    public void BillAddPackage() {  //往 billtable的子表中插入 数据 :id bill_table_id packaget_id sum







    }

    @RabbitHandler
    @RabbitListener(queues = "admin_Bill_queue")
    public void BillAddBYCardInfoCopy(String msg2) {
        if (StringUtils.isEmpty(msg2)) {
            return;
        }
        Map<String, Object> Pmap = JSON.parseObject(msg2);
        String fistTime = Pmap.get("fist").toString();//当月第一时间
        String lastTime = Pmap.get("last").toString();//当月最后的时间

        List<Map<String,Object>> depts = yzBillMapper.selDept();//查询所有 dept_id
        for (int i = 0; i < depts.size(); i++) {
            Map<String, Object> selMap = depts.get(i);
            try{
                selMap.put("fistTime",fistTime);//放入本月第一天
                selMap.put("lastTime",lastTime);//放入本月最后一天
                Map<String, Object> countMaps = yzBillMapper.selCardCountByDeptId(selMap);//卡总数
                selMap.put("count",countMaps.get("card_count").toString());//卡总数放入
                Map<String, Object> active = yzBillMapper.selCardActiveByTime(selMap);//激活数量
                selMap.put("activeCount",active.get("card_active_count").toString());//激活数量放入
                Map<String, Object> halt = yzBillMapper.selCardHaltByDept(selMap);//停机数量
                selMap.put("HaltCount",halt.get("HaltCount").toString());//停机数量放入
                Map<String, Object> NotActive = yzBillMapper.selNotActiveByDept(selMap);//未激活数量
                selMap.put("NotActiveCount",NotActive.get("NotActiveCount").toString());//未激活数量放入
                try{
                    Map<String, Object> Used = yzBillMapper.selUsed(selMap);//当月总用量
                    selMap.put("Used",Used.get("Used").toString());//当月总用量放入

                }catch(Exception e){
                    selMap.put("Used",0.0);//当月总用量放入
                }
                try{
                    Map<String, Object> remaining = yzBillMapper.selRemaining(selMap); //当月剩余总用量
                    selMap.put("remaining",remaining.get("remaining").toString());//当月剩余总用量放入

                }catch(Exception e){
                    selMap.put("remaining",0.0);//当月剩余总用量放入
                }

                //selMap:{fistTime:xxxx,lastTime:xxx,dept_id:xxx}
                //插入到 yz_bill_table
                yzBillMapper.insertBillTable(selMap);//selMap中存放yz_bill_table 主键

                List<String> packets = yzBillsMapper.selPacketByOrder(selMap);//根据dept_id 查询 都有哪些 packet_id
                String dept_id = selMap.get("dept_id").toString();
                String id = selMap.get("id").toString();
                for (int j = 0; j < packets.size(); j++) {
                    // todo 判断 查到的 packetid是否为空
                    if (packets.get(j) == null){
                     continue;
                    }
                    HashMap<String, Object> instrMap = new HashMap<>();
                    instrMap.put("id",id);// 主表id
                    instrMap.put("dept_id",dept_id);//dept_id
                    instrMap.put("packet_id",packets.get(j));//packet_id

                    //遍历所有的 packet_id 根据packet获取 iccid
                    HashMap<String, Object> selIccidMap = new HashMap<>();

                    selIccidMap.put("dept_id",dept_id);
                    selIccidMap.put("packet_id",packets.get(j));
                    List<String> Iccids = yzBillsMapper.selIccidByPacket(selIccidMap);
                    // todo  去重
                    Set<String> strings = new HashSet<>();
                    strings.addAll(Iccids);
                    List arrayList = new ArrayList();
                    arrayList.addAll(strings);
                    instrMap.put("count",arrayList.size());//卡数量
                    if (arrayList.size() != 0){
                        Map<String, Object> selCountMap = new HashMap<>();
                        selCountMap.put("dept_id",dept_id);
                        selCountMap.put("iccids",arrayList);
                        Map<String, Object> haltmap = yzBillsMapper.selHaltCount(selCountMap);// sqlE
                        instrMap.put("haltcount",haltmap.get("haltcount"));//停机数量
                        Map<String, Object> notactivemap = yzBillsMapper.selNotActiveCount(selCountMap);
                        instrMap.put("notactivecount",notactivemap.get("notactivecount"));//未激活数量

                        selCountMap.put("fistTime",fistTime);
                        selCountMap.put("lastTime",lastTime);
                        Map<String, Object> activemap = yzBillsMapper.selActiveCountInMonth(selCountMap);
                        instrMap.put("activecount",activemap.get("activecount"));//激活数量

                        Map<String, Object> maps = yzBillsMapper.selUsedAndRemaining(Iccids);
                        try{
                            instrMap.put("used",maps.get("used"));//总用量
                        }catch(Exception e){
                            instrMap.put("used",0.0);//总用量
                        }
                        try{
                            instrMap.put("remaining",maps.get("remaining"));//总剩余
                        }catch(Exception e){
                            instrMap.put("remaining",0.0);//总剩余
                        }

                    }else {
                        instrMap.put("haltcount",0);//停机数量
                        instrMap.put("notactivecount",0);//未激活数量
                        instrMap.put("activecount",0);//激活数量
                        instrMap.put("used",0.0);//总用量
                        instrMap.put("remaining",0.0);//总剩余
                    }


                    yzBillsMapper.inTable(instrMap);
                    //System.out.println(instrMap); // 输出 展示
                }
            }catch(Exception e){
                e.printStackTrace();
                continue;
            }

            log.info("账单 插入 完成 dept_id:{} 企业.",depts.get(i).get("dept_id"));

        }






        }





}
